package com.bytecodezz.app.sketch.draw.model

import android.os.Build
import android.view.View
import android.view.animation.Animation
import android.view.animation.BounceInterpolator
import android.view.animation.TranslateAnimation
import androidx.annotation.RequiresApi
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import com.bytecodezz.app.base.framework.better.SingleLiveEvent
import com.bytecodezz.app.sketch.common.base.BaseUIViewModel

/**
 * @author: bytecodezz
 * @version: 1.0
 *
 * @description:
 *
 */
class DrawViewModel : BaseUIViewModel() {
    //是否该响应隐藏View，目前在动画结束时会对其进行设置
    val actionViewIsHide = SingleLiveEvent<Boolean>()

    private val actionBarShowAnimation: TranslateAnimation by lazy {
        TranslateAnimation(
            Animation.RELATIVE_TO_SELF,  //RELATIVE_TO_SELF表示操作自身
            0.0f,  //fromXValue表示开始的X轴位置
            Animation.RELATIVE_TO_SELF,
            0.0f,  //fromXValue表示结束的X轴位置
            Animation.RELATIVE_TO_SELF,
            -1.0f,  //fromXValue表示开始的Y轴位置
            Animation.RELATIVE_TO_SELF,
            0.0f //fromXValue表示结束的Y轴位置
        ).also {
            it.interpolator = BounceInterpolator()
            it.repeatMode = Animation.REVERSE
            it.duration = 300L
        }
    }
    private val actionBarHideAnimation: TranslateAnimation by lazy {
        TranslateAnimation(
            Animation.RELATIVE_TO_SELF,  //RELATIVE_TO_SELF表示操作自身
            0.0f,  //fromXValue表示开始的X轴位置
            Animation.RELATIVE_TO_SELF,
            0.0f,  //fromXValue表示结束的X轴位置
            Animation.RELATIVE_TO_SELF,
            0.0f,  //fromXValue表示开始的Y轴位置
            Animation.RELATIVE_TO_SELF,
            -1.0f //fromXValue表示结束的Y轴位置
        ).also {
            it.repeatMode = Animation.REVERSE
            it.duration = 250L
            it.setAnimationListener(object : Animation.AnimationListener {
                override fun onAnimationStart(animation: Animation) {}
                override fun onAnimationEnd(animation: Animation) {
                    actionViewIsHide.value = true
                }

                override fun onAnimationRepeat(animation: Animation) {}
            })
        }
    }

    fun fullScreenActionAnimation(intoFull: Boolean, target: View) {
        if (intoFull) {
            target.startAnimation(actionBarHideAnimation)
        } else {
            actionViewIsHide.value = false
            target.startAnimation(actionBarShowAnimation)
        }
    }

    fun initDrawStart() {
    }

    //动态隐藏 系统状态栏和底部导航栏
    fun initSystemActionBar(decorView: View) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            // API 30 以上
            hideSystemActionBarGreaterR(decorView)
        } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
            // API 30以下
            hideSystemActionBarLessR(decorView)
        }
    }

    private fun hideSystemActionBarLessR(decorView: View) {
        decorView.systemUiVisibility =
            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or View.SYSTEM_UI_FLAG_FULLSCREEN or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION

    }

    @RequiresApi(Build.VERSION_CODES.R)
    private fun hideSystemActionBarGreaterR(decorView: View) {
        val insetsController = ViewCompat.getWindowInsetsController(decorView)
        insetsController?.hide(WindowInsetsCompat.Type.systemBars())
    }
}